/**
 * synchronized关键字
 * 对某个对象加锁
 * @author mashibing
 */

package com.concurrent.study.yxxy.c_001;

public class T {
	
	private int count = 10;
	private Object o = new Object();
	
	public void m() {
		synchronized(o) { //任何线程要执行下面的代码，必须先拿到o的锁
			count--;
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " count = " + count);
		}
	}

	public void m1() {
		count--;
		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + " count = " + count);
	}

	public static void main(String[] args) {
		/*T t1 = new T();
		new Thread(t1::m,"t1").start();*/
		T t1 = new T();
		for(int i=0; i<20 ;i++){
			new Thread(t1::m,"t"+i).start();
		}
	}
	
}

